package com.szy.douyou_article.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szy.douyou_article.config.AccountProfile;
import com.szy.douyou_article.config.ResultDTO;
import com.szy.douyou_article.dao.Users;
import com.szy.douyou_article.dao.req.UserReq;
import com.szy.douyou_article.service.UsersService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author sunziyan
 * @date 2022/4/4
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Resource
    private UsersService usersService;

    @RequiresAuthentication
    @PostMapping("/updateMyMessage")
    public ResultDTO<Users> UpdateMyMessage(@RequestBody Users user) {
        return usersService.updateOneUser(user);
    }

    @RequiresAuthentication
    @PostMapping("/queryPage")
    public ResultDTO<Page<Users>> queryPage(@RequestBody UserReq userReq) {
        return usersService.queryUser(userReq);
    }

    @RequiresAuthentication
    @GetMapping("/deleteUser/{userId}")
    public ResultDTO deleteUser(@PathVariable("userId") Long userId) {
        Subject subject = SecurityUtils.getSubject();
        AccountProfile principal = (AccountProfile) subject.getPrincipal();

        if(principal.getUserRight() == 1){
            return usersService.deleteUser(userId);
        }else {
            return ResultDTO.fail("没有管理员权限");
        }
    }

    @GetMapping("/getOneUser/{userId}")
    public ResultDTO getOneUser(@PathVariable("userId")Long userId){
        return ResultDTO.success(usersService.getUserById(userId));
    }




}
